<?php
/**
 * @param str text: Text with references.
 * @return str: Text localized.
 */
function BME_localize($text) {
	while ( parseLocalization($text) ):
		// :)
	endwhile;
	return $text;
}
/**
 * @param str &text: Text with references.
 * @return str: number of changes made.
 */
function parseLocalization(&$text) {
	global $_U,$_lang;
	$i = 0;
	$_lang = $_COOKIE["MP_lang"]?$_COOKIE["MP_lang"]:"es";
	
	preg_match_all("/%%_([A-Za-z0-9\.]+)%%/",$text,$fTags);
	foreach ( $fTags[1] as $fTag ):
		if ( isset($_U[$fTag]) ):
			$text = str_replace("%%_$fTag%%",$_U[$fTag],$text);
			$i++;
		endif;
	endforeach;
	
	preg_match_all("/%%([A-Za-z0-9\.][A-Za-z0-9\._]*)%%/",$text,$fTags);
	foreach ( $fTags[1] as $fTag ):
		if ( ($tTag=getLocal($fTag))!==NULL ):
			$text = str_replace("%%$fTag%%",$tTag,$text);
			$i++;
		endif;
	endforeach;
	return $i;
}
/**
 * @param str tok: The token identifying the local string.
 * @return mix: Local value for the string (str) or NULL (null) if not defined.
 */
function getLocal($tok) {
	global $bmelcl;
	return $bmelcl[$tok];
}
?>
